#!/bin/bash

# 定义变量
WAR_FILE="ssm3j93s.war"
WEB_PATH="ssm3j93s"
TOMCAT_IMAGE="tomcat:9-jdk17-openjdk-slim"
CONTAINER_NAME="ssm-tomcat"
TOMCAT_WEBAPPS="/usr/local/tomcat/webapps"

# 检查war文件是否存在
if [ ! -f "./target/$WAR_FILE" ]; then
    echo "错误: ./target/$WAR_FILE 文件不存在!"
    exit 1
fi

# 停止并删除已存在的容器(如果存在)
echo "停止并删除已存在的容器..."
docker stop $CONTAINER_NAME >/dev/null 2>&1
docker rm $CONTAINER_NAME >/dev/null 2>&1

# 运行新的tomcat容器并部署war文件
echo "启动tomcat容器并部署./target/$WAR_FILE..."
docker run -d \
    --name $CONTAINER_NAME \
    -p 8080:8080 \
    -v $(pwd)/target/$WAR_FILE:$TOMCAT_WEBAPPS/$WAR_FILE \
    $TOMCAT_IMAGE

echo "部署完成!"
echo "访问地址: http://localhost:8080/$WEB_PATH"